package org.hiucung.lib_voice.manager

import android.content.Context
import android.util.Log
import com.baidu.speech.EventListener
import com.baidu.speech.asr.SpeechConstant
import com.baidu.speech.utils.LogUtil
import org.hiucung.lib_voice.asr.VoiceAsr
import org.hiucung.lib_voice.impl.OnAsrResultListener
import org.hiucung.lib_voice.tts.VoiceTTS
import org.hiucung.lib_voice.wakeup.VoiceWakeUp
import org.json.JSONObject

/**
 * @title: VoiceManager
 * @projectName OrangeAiVoiceApp
 * @description: TODO
 * @author Kicc
 * @date 2021/11/19 19:11
 */
object VoiceManager : EventListener {

    private var TAG = VoiceManager::class.java.simpleName

    //语音key
    const val VOICE_APP_ID = "25189685"
    const val VOICE_APP_KEY = "sV3fHsRizpYutbxhwGbtoMqd"
    const val VOICE_APP_SECRET = "8U6WxS6FpgTNn7FDG3GoH5K9INhbbBWF"

    //接口
    private lateinit var mOnAsrResultListener: OnAsrResultListener

    fun initManager(mContext: Context, mOnAsrResultListener: OnAsrResultListener) {
        this.mOnAsrResultListener = mOnAsrResultListener

        VoiceTTS.initTTS(mContext)
        VoiceWakeUp.initWakeUp(mContext, this)
        VoiceAsr.initAsr(mContext, this)
    }

    //------------------------TTS START------------------------
    //播放
    fun ttsStart(text: String) {
        VoiceTTS.start(text, null)
    }

    //播放并监听
    fun ttsStart(text: String, onTTSResultListener: VoiceTTS.OnTTSResultListener) {
        VoiceTTS.start(text, onTTSResultListener)
    }

    //暂停
    fun ttsPause() {
        VoiceTTS.pause()
    }

    //继续播放
    fun ttsResume() {
        VoiceTTS.resume()
    }

    //停止播放
    fun ttsStop() {
        VoiceTTS.stop()
    }

    //释放资源
    fun ttsRelease() {
        VoiceTTS.release()
    }

    //设置发音人
    fun setPeople(people: String) {
        VoiceTTS.setPeople(people)
    }

    //设置语速
    fun setVoiceSpeed(speed: String) {
        VoiceTTS.setVoiceSpeed(speed)
    }

    //设置音量
    fun setVoiceVolume(volume: String) {
        VoiceTTS.setVoiceVolume(volume)
    }

    //------------------------TTS END------------------------

    //------------------------WakeUp Start------------------------
    fun startWakeUp() {
        LogUtil.i("开始语音唤醒")
        VoiceWakeUp.startWakeUp()
    }

    fun stopWakeUp() {
        LogUtil.i("结束语音唤醒")
        VoiceWakeUp.stopWakeUp()
    }
    //------------------------WakeUp END------------------------

    //------------------------ASR START------------------------

    fun startAsr() {
        LogUtil.i("开始语音识别")
        VoiceAsr.startAsr()
    }

    fun stopAsr() {
        LogUtil.i("停止语音识别")
        VoiceAsr.stopAsr()
    }

    fun cancelAsr() {
        LogUtil.i("取消语音识别")
        VoiceAsr.cancelAsr()
    }

    fun releaseAsr() {
        LogUtil.i("释放语音识别")
        VoiceAsr.releaseAsr(this)
    }

    //------------------------ASR END------------------------

    override fun onEvent(
        name: String?,
        parames: String?,
        byte: ByteArray?,
        offset: Int,
        length: Int
    ) {

        when (name) {
            SpeechConstant.CALLBACK_EVENT_WAKEUP_READY -> {
                Log.i(TAG, "唤醒准备就绪")
                mOnAsrResultListener.wakeUpReady()
            }
            SpeechConstant.CALLBACK_EVENT_ASR_BEGIN -> {
                Log.i(TAG, "开始说话")
                mOnAsrResultListener.asrStartSpeak()
            }
            SpeechConstant.CALLBACK_EVENT_ASR_END -> {
                Log.i(TAG, "结束说话")
                mOnAsrResultListener.asrStopSpeak()
            }
        }

        //去除脏数据
        if (parames == null) {
            return
        }

        val allJson = JSONObject(parames)
        Log.i("Test", "allJson:$name$allJson")
        when (name) {
            SpeechConstant.CALLBACK_EVENT_WAKEUP_SUCCESS -> {
//                ttsStart("我在，有什么可以帮到您？")
                mOnAsrResultListener.wakeUpSuccess(allJson)
            }
            SpeechConstant.CALLBACK_EVENT_WAKEUP_ERROR -> {
                Log.i(TAG, "唤醒失败")
                mOnAsrResultListener.voiceError("唤醒失败")
            }
            SpeechConstant.CALLBACK_EVENT_ASR_FINISH -> {
                mOnAsrResultListener.asrResult(allJson)
            }
            SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL -> {
                mOnAsrResultListener.updateUserText(allJson.optString("best_result"))
                byte?.let {
                    val nlu = JSONObject(String(it, offset, length))
                    mOnAsrResultListener.asrResult(nlu)
                }
            }
        }
    }

}